package com.ruoyi.web.controller.portal;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.portal.domain.PorNews;
import com.ruoyi.portal.service.IPorNewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 新闻Controller
 * 
 * @author cjie
 * @date 2021-05-09
 */
@RestController
@RequestMapping("/portal/news")
public class PorNewsController extends BaseController {
    @Autowired
    private IPorNewsService porNewsService;

    /**
     * 查询新闻列表
     */
    @PreAuthorize("@ss.hasPermi('portal:news:list')")
    @GetMapping("/list")
    public TableDataInfo list(PorNews porNews) {
        startPage();
        List<PorNews> list = porNewsService.selectPorNewsList(porNews);
        return getDataTable(list);
    }

    /**
     * 导出新闻列表
     */
    @PreAuthorize("@ss.hasPermi('portal:news:export')")
    @Log(title = "新闻", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(PorNews porNews) {
        List<PorNews> list = porNewsService.selectPorNewsList(porNews);
        ExcelUtil<PorNews> util = new ExcelUtil<PorNews>(PorNews.class);
        return util.exportExcel(list, "新闻数据");
    }

    /**
     * 获取新闻详细信息
     */
    @PreAuthorize("@ss.hasPermi('portal:news:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id) {
        return AjaxResult.success(porNewsService.selectPorNewsById(id));
    }

    /**
     * 新增新闻
     */
    @PreAuthorize("@ss.hasPermi('portal:news:add')")
    @Log(title = "新闻", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PorNews porNews) {
        return toAjax(porNewsService.insertPorNews(porNews));
    }

    /**
     * 修改新闻
     */
    @PreAuthorize("@ss.hasPermi('portal:news:edit')")
    @Log(title = "新闻", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PorNews porNews) {
        return toAjax(porNewsService.updatePorNews(porNews));
    }

    /**
     * 删除新闻
     */
    @PreAuthorize("@ss.hasPermi('portal:news:remove')")
    @Log(title = "新闻", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids) {
        return toAjax(porNewsService.deletePorNewsByIds(ids));
    }
}
